// 2. **语音转文字功能 (useVoiceToText.js)**:

import { ref } from 'vue'

export function useVoiceToText() {
  const text = ref('')
  let recognition = null

  // 检查浏览器支持
  if ('webkitSpeechRecognition' in window) {
    recognition = new webkitSpeechRecognition()
    recognition.continuous = true
    recognition.interimResults = true
    recognition.lang = 'zh-CN'

    recognition.onresult = (event) => {
      const result = event.results[event.results.length - 1]
      if (result.isFinal) {
        text.value = result[0].transcript
      }
    }
  }

  const startRecord = () => {
    if (recognition) {
      recognition.start()
    } else {
      console.warn('该浏览器不支持语音识别')
    }
  }

  const stopRecord = () => {
    if (recognition) {
      recognition.stop()
    }
  }

  return {
    text,
    startRecord,
    stopRecord,
  }
}
